//
//  PollingTableViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/13.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingTableViewController.h"
#import "EnvironmentDetailView.h"
#import "EnvironmentDetailCell.h"
#import "TPKeyboardAvoidingTableView.h"
#import "EnvironmentEditTableCell.h"
#import "PollingIdeaViewController.h"
#import "PollingDetailViewController.h"
@interface PollingTableViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)TPKeyboardAvoidingTableView*table;
@property(nonatomic,strong)NSMutableArray*textFieldAr;
@property(nonatomic,strong)NSMutableArray*selectAr;
@property(nonatomic,strong)NSMutableArray*dataTableAr;
@property(nonatomic,strong)UIViewController*tableViewController;
@property(nonatomic)NSInteger index;
@property(nonatomic)NSInteger selectRow;;
@property(nonatomic,strong)UILabel *rightLab;
@property(nonatomic,strong)UIView *rightView;
@property(nonatomic,strong)EnvironmentDetailView *environView;
@property(nonatomic,strong)NSMutableDictionary *nameDic;
@end

@implementation PollingTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    self.nameDic=self.passDic;
    _selectRow=10000;
    _textFieldAr=[[NSMutableArray alloc]init];
    _selectAr=[[NSMutableArray alloc]init];
    _dataTableAr=[[NSMutableArray alloc]init];
    //对应的位置
    _index=[[self.passDic objectForKey:@"order_no"]integerValue];
    self.navBar.leftView = nil;
    [self.navBar setHidden:YES];
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        
        NSMutableArray *parameterAr=[[NSMutableArray alloc]init];
        for (int i=0; i<self.dataTableAr.count; i++) {
            
            NSMutableDictionary *upDic=[[NSMutableDictionary alloc]init];//上传的参数
            NSMutableDictionary *askDic=[self.dataTableAr objectAtIndex:i];//请求数据
            [upDic setObject:[askDic objectForKey:@"item_uuid"] forKey:@"item_uuid"];//巡检项uuid
            //找到正常异常
            NSString* num=[self.selectAr objectAtIndex:i];
            //上传到 选择项
            if ([num isEqualToString:@"1"]) {//正常
                [upDic setObject:@"1" forKey:@"result"];
                
            }else if([num isEqualToString:@"2"])//异常
            {
                [upDic setObject:@"2" forKey:@"result"];
                
            }else if ([num isEqualToString:@"0"])//没有填的
            {
                //没有选择 选择一个框
                [upDic setObject:@"" forKey:@"result"];
                
            }
            
            NSString* textFiled=[self.textFieldAr objectAtIndex:i];
            [upDic setObject:textFiled  forKey:@"content"];
            [parameterAr addObject:upDic];
        }
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:parameterAr forKey:@"list"];
        [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
        NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            
            
            UINavigationController *navcontroller = self.navigationController;
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
            
            for (UIViewController *temp in viewControllers)
            {
                if ([temp isKindOfClass:[PollingDetailViewController class]])
                {
                    [self.navigationController popToViewController:temp animated:YES];
                }
            }
            
        } onError:^(BaseModel *model) {
            
            UINavigationController *navcontroller = self.navigationController;
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
            
            for (UIViewController *temp in viewControllers)
            {
                if ([temp isKindOfClass:[PollingDetailViewController class]])
                {
                    [self.navigationController popToViewController:temp animated:YES];
                }
            }
        }];

    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];

    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 70,44);
    [backTitle setText:@"巡检详情"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    
   UILabel*titleLab =[[UILabel alloc]init];
    [narView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(narView);
        make.top.equalTo(narView).offset(30);
    }];
    titleLab.font =F(16);
    titleLab.textColor =C(@"5a5a5a");
    titleLab.text=[self.nameDic objectForKey:@"name"];
    
    _rightView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-90, 0, 80, 64)];
    [narView addSubview:_rightView];
    _rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 44)];
    _rightLab.textAlignment=NSTextAlignmentRight;
    _rightLab.font=F(14);

    _rightLab.userInteractionEnabled=YES;
    _rightLab.textColor=MainColor;
    [_rightView addSubview:_rightLab];
    
    [_rightView bk_whenTapped:^{
        if ([_rightLab.text isEqualToString:@"修改"]) {
            [_environView removeFromSuperview];
            _rightLab.text=@"确定";
            [self createEdit];
        }else if ([_rightLab.text isEqualToString:@"确定"])
        {

            NSMutableArray *parameterAr=[[NSMutableArray alloc]init];
            for (int i=0; i<self.dataTableAr.count; i++) {
                
                NSMutableDictionary *upDic=[[NSMutableDictionary alloc]init];//上传的参数
                NSMutableDictionary *askDic=[self.dataTableAr objectAtIndex:i];//请求数据
                [upDic setObject:[askDic objectForKey:@"item_uuid"] forKey:@"item_uuid"];//巡检项uuid
                //找到正常异常
                NSString* num=[self.selectAr objectAtIndex:i];
                //上传到 选择项
                if ([num isEqualToString:@"1"]) {//正常
                    [upDic setObject:@"1" forKey:@"result"];
                    
                }else if([num isEqualToString:@"2"])//异常
                {
                    [upDic setObject:@"2" forKey:@"result"];
                    
                }else if ([num isEqualToString:@"0"])//没有填的
                {
                    //没有选择 选择一个框
                    //                [upDic setObject:@"" forKey:@"result"];
                    _selectRow=i;
                    _table.contentOffset=CGPointMake(0, i*130);
                    [_table reloadData];
                    return ;
                    
                    
                }
                
                //找到必填项
                NSMutableDictionary *necessaryDic = [NSMutableDictionary dictionaryWithDictionary:self.dataTableAr[i]];
                NSString* textFiled=[self.textFieldAr objectAtIndex:i];
                
                if ([[necessaryDic objectForKey:@"is_required"] isEqualToString:@"y"]) {//备注是否必填	'y' - 是
                    if ([textFiled isEqualToString:@""]) {
                        NSString *name=[NSString stringWithFormat:@"请输入%@的具体数值！",[askDic objectForKey:@"name"]];
                        _selectRow=i;
                        _table.contentOffset=CGPointMake(0, i*130);
                        [_table reloadData];

                        [CClass showMessage:name ];
                        return ;
                    }else
                    {
                        [upDic setObject:textFiled  forKey:@"content"];//上传备注
                        
                    }
                    
                }else
                {
                    
                    [upDic setObject:textFiled  forKey:@"content"];
                    
                }
                
                [parameterAr addObject:upDic];
                
                
            }
            
            NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
            [dataDic setObject:parameterAr forKey:@"list"];
            [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
            NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
                
                
                //进入下个界面
                //isEdit 0 为没有填完 1为填完
                [self gotoNext];
                
            } onError:^(BaseModel *model) {
                
            }];
            
            
    

        }
     
    }];
  
   
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, narView.bounds.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
    [bottomLineView setBackgroundColor:LineColor];
    [narView addSubview:bottomLineView];
    
    
 NSString*isEdit=[self.passDic objectForKey:@"isEdit"];

    //isEdit 0 为没有填完 1为填完
    if([isEdit isEqualToString:@"0"])
    {
        [self createEdit];
        _rightLab.text =@"确定";

    }else
    {
        _rightLab.text =@"修改";
        [self createDetail];
    }
}
- (void) createDetail
{
    _environView=[[EnvironmentDetailView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74) withUuid:self.Uuid WithItemUuid:[self.nameDic objectForKey:@"uuid"] withIndex:_index];
    [self.view addSubview:_environView];
    __block typeof (self)blockSelf=self;

    //上个界面
    [_environView setGotoUpward:^(NSString *string) {
        [blockSelf gotoUp];
    }];
    //下个界面
    [_environView setGotoDownward:^(NSString *string) {
        
        [blockSelf gotoNext];
    }];
    
    

    

}
- (void)createEdit
{
   
    _table =[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth,WindowHeight-64)];
    _table.delegate =self;
    _table.dataSource =self;
    _table.backgroundColor =BackColor;
    _table.separatorStyle =UITableViewCellSelectionStyleNone;
    [self.view addSubview:_table];
    [_table registerClass:[EnvironmentEditTableCell class] forCellReuseIdentifier:@"cell"];
    
    [CClass HttpRequestTask:YES withUrl:@"inspection_getSubReport" withData:@{@"uuid":self.Uuid,@"item_uuid":[self.nameDic objectForKey:@"uuid"]} notifyError:YES onSuccess:^(BaseModel *model) {
//        [_selectAr removeAllObjects];
//        [_textFieldAr removeAllObjects];
//        [_dataTableAr removeAllObjects];
        self.dataTableAr=[model.B_data objectForKey:@"list"];
        //通过index处理上一页 下一页的显示
        for (int i=0; i<self.dataTableAr.count; i++) {
            

            NSMutableDictionary *dadaDic=[[NSMutableDictionary alloc]init];
            dadaDic =[self.dataTableAr objectAtIndex:i];
            //选择的数据
            if ([F_NSpace([dadaDic objectForKey:@"result"]) isEqualToString:@""]) {
                [_selectAr addObject:@"0"];

            }else
            {

                if ([[dadaDic objectForKey:@"result"] integerValue] ==1) {
                    [_selectAr addObject:@"1"];//正常

                    
                }else if([[dadaDic objectForKey:@"result"] integerValue] ==2)
                {
                    [_selectAr addObject:@"2"];

                }else
                {
                    [_selectAr addObject:@"0"];

                }
                
            }
            //备注数据
            if ([F_NSpace([dadaDic objectForKey:@"content"]) isEqualToString:@""]) {
                [_textFieldAr addObject:@""];
            }else
            {
                [_textFieldAr addObject:[dadaDic objectForKey:@"content"]];
            }
            
            
        }

        [_table reloadData];

        _table.tableFooterView= [self footTableView];
        
        
    } onError:^(BaseModel *model) {
        
    }];

    

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataTableAr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld", (long)[indexPath section], (long)[indexPath section]];//以indexPath来唯一确定cell

    EnvironmentEditTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[EnvironmentEditTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    if (_selectRow==indexPath.section) {
        
        cell.contentView.backgroundColor=C(@"fdede7");
        cell.line1.backgroundColor=C(@"f16d4f");
        cell.line2.backgroundColor=C(@"f16d4f");


    }else
    {
       
        cell.contentView.backgroundColor=[UIColor whiteColor];
        cell.line1.backgroundColor=C(@"cdcdcd");
        cell.line2.backgroundColor=C(@"cdcdcd");

    }
    NSMutableDictionary *dic=[self.dataTableAr objectAtIndex:indexPath.section];
    NSString *name;
    if ([F_NSpace([dic objectForKey:@"name"]) isEqualToString:@""]) {
        cell.nameLab.text=@"";
        name=@"";
    }else
    {
        cell.nameLab.text=[dic objectForKey:@"name"];
        name=[dic objectForKey:@"name"];
 
    }
    
    
    if ([F_NSpace([dic objectForKey:@"description"]) isEqualToString:@""]) {
        cell.contexLab.text=@"";

    }else
    {
        cell.contexLab.text=[dic objectForKey:@"description"];

        
    }

    [cell.selectView.firstBtn setTitle:[dic objectForKey:@"good_text"] forState:UIControlStateNormal];
    [cell.selectView.secondBtn setTitle:[dic objectForKey:@"bad_text"] forState:UIControlStateNormal];
    if ([[dic objectForKey:@"is_required"] isEqualToString:@"y"]) {//备注是否必填	'y' - 是
        cell.startLab.text=@"*";
        
    }else
    {
        cell. startLab.text=@"";
        
    }
    
    if ([F_NSpace([dic objectForKey:@"prompt"]) isEqualToString:@""]) {
        cell.textField.placeholder=@"备注信息...";
        
    }else
    {
        cell.textField.placeholder=[dic objectForKey:@"prompt"];
        
    }
    
   
    
    //从数组中取值
    NSString *textFiled=[self.textFieldAr objectAtIndex:indexPath.section];
    cell.textField.delegate=self;
    if ([textFiled isEqualToString:@""]) {
        cell.textField.text=@"";
        
    }else
    {
        cell.textField.text= textFiled;
        
    }
    NSInteger num=[[self.selectAr objectAtIndex:indexPath.section] integerValue];
    
    if (num==0) {
        [cell.selectView.firstBtn setImage:[UIImage imageNamed:@"Ico_input_NoSelect"] forState:UIControlStateNormal];
        [cell.selectView.secondBtn setImage:[UIImage imageNamed:@"Ico_input_NoSelect"] forState:UIControlStateNormal];
        
    }else if(num==1)
    {
        [cell.selectView.firstBtn setImage:[UIImage imageNamed:@"Ico_input_select"] forState:UIControlStateNormal];
        [cell.selectView.secondBtn setImage:[UIImage imageNamed:@"Ico_input_NoSelect"] forState:UIControlStateNormal];
    }else
    {
        [cell.selectView.firstBtn setImage:[UIImage imageNamed:@"Ico_input_NoSelect"] forState:UIControlStateNormal];
        [cell.selectView.secondBtn setImage:[UIImage imageNamed:@"Ico_input_select"] forState:UIControlStateNormal];
    }
//    if(WindowHeight==480)
//    {
//        [cell.textField  bk_addEventHandler:^(UITextField *tfd) {
//            
// 
//            
//            
//        } forControlEvents:UIControlEventEditingChanged];
//        
//
//    }else
//    {
        [cell.textField setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *tfd, NSRange range, NSString * str) {
            if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
                return NO;
            }
            NSString *textString = [tfd.text stringByReplacingCharactersInRange:range withString:str];

            if (textString.length>20) {
                return NO;
            }else
            {
//                if(iOS7Later)
//                {
//                    
//                 [self.textFieldAr replaceObjectAtIndex:indexPath.section withObject:textString];
//                    
//                }else
//                {
//                    
//                [self.textFieldAr replaceObjectAtIndex:indexPath.section withObject:tfd.text];
//              
//                }
//                
                
                return YES;
            }
            
            
        }];

//    [cell.textField setBk_didEndEditingBlock:^(UITextField *textFiled) {
//        [CClass stringContainsEmoji:textFiled.text];
//
//    }];

   
    [cell.textField bk_addEventHandler:^(id sender) {

        UITextPosition *position = [cell.textField positionFromPosition:[cell.textField markedTextRange] offset:0];
        if (position) {
            return;
        }
        NSInteger maxLength = 20;
        if (maxLength > 0) {
            NSInteger splitcount;
            if (splitcount > 0) {
                maxLength = maxLength + maxLength /splitcount - (maxLength % splitcount == 0 ? 1 : 0);
            }
            if (cell.textField.text.length > maxLength) {
                cell.textField.text = [cell.textField.text substringWithRange:NSMakeRange(0, maxLength)];
            }
            //移除表情
           NSString *newStr= [CClass disable_emoji:cell.textField.text];
            cell.textField.text=newStr;
            [self.textFieldAr replaceObjectAtIndex:indexPath.section withObject:cell.textField.text];

          

        }

    } forControlEvents:UIControlEventEditingChanged];
    
    [cell.selectView setSelectBtnClick:^(NSString *num) {
        if ([num isEqualToString:@"1"]) {
//            NSLog(@"----正常");
            
        }else if([num isEqualToString:@"2"])
        {
//            NSLog(@"----异常");
            
        }
        [self.selectAr replaceObjectAtIndex:indexPath.section withObject:num];
        
    }];
    

    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic=[self.dataTableAr objectAtIndex:indexPath.section];
    
    if ([F_NSpace([dic objectForKey:@"description"]) isEqualToString:@""]) {
        return 130;
        
    }else
    {
        CGSize equitmetSize  = [[dic objectForKey:@"description"] sizeWithFont:F(11) constrainedToSize:CGSizeMake(WindowWidth-30, 900)];

        return 130+equitmetSize.height+5;
        
        
    }

}
- (UIView*)footTableView
{    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 90)];
    view.backgroundColor =BackColor;
    UIButton*upBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:upBtn];
    CGFloat widtnBtn=(WindowWidth-60-40)/2;
    upBtn.frame=CGRectMake(30, 25, widtnBtn, 30);
    [upBtn setBackgroundColor:MainColor];
    upBtn.layer.masksToBounds=YES;
    upBtn.layer.cornerRadius=6;
    upBtn.titleLabel.font=F(16);
    [upBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [upBtn bk_addEventHandler:^(id sender) {
        [self gotoUp];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton*downBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:downBtn];
    [downBtn setBackgroundColor:MainColor];
    downBtn.layer.masksToBounds=YES;
    downBtn.layer.cornerRadius=6;
    downBtn.titleLabel.font=F(16);
    [downBtn setTitle:@"下一页" forState:UIControlStateNormal];
    [downBtn bk_addEventHandler:^(id sender) {
        
        [self gotoNext];
    } forControlEvents:UIControlEventTouchUpInside];
    if (_index==1) {
        
        upBtn.hidden=YES;
        downBtn.frame=CGRectMake(30, 25, WindowWidth-60, 30);
        
    }else
    {
        upBtn.hidden=NO;
        upBtn.frame=CGRectMake(30, 25, widtnBtn, 30);
        downBtn.frame=CGRectMake(WindowWidth-30-widtnBtn, 25, widtnBtn, 30);
        
    }
    return view;
}
#pragma mark 下一页
- (void)gotoNext
{
    
    _index++;
    if (_index==0) {
        //返回到表格列表
        [self backController];
        
    }else if(_index>self.allAr.count)
    {
        NSMutableArray *parameterAr=[[NSMutableArray alloc]init];
        for (int i=0; i<self.dataTableAr.count; i++) {
            
            NSMutableDictionary *upDic=[[NSMutableDictionary alloc]init];//上传的参数
            NSMutableDictionary *askDic=[self.dataTableAr objectAtIndex:i];//请求数据
            [upDic setObject:[askDic objectForKey:@"item_uuid"] forKey:@"item_uuid"];//巡检项uuid
            //找到正常异常
            NSString* num=[self.selectAr objectAtIndex:i];
            //上传到 选择项
            if ([num isEqualToString:@"1"]) {//正常
                [upDic setObject:@"1" forKey:@"result"];
                
            }else if([num isEqualToString:@"2"])//异常
            {
                [upDic setObject:@"2" forKey:@"result"];
                
            }else if ([num isEqualToString:@"0"])//没有填的
            {
                //没有选择 选择一个框
                [upDic setObject:@"" forKey:@"result"];
                
                
            }
            
            NSString* textFiled=[self.textFieldAr objectAtIndex:i];
            [upDic setObject:textFiled  forKey:@"content"];
            [parameterAr addObject:upDic];
            
            
        }
        
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:parameterAr forKey:@"list"];
        [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
        NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            //先保存进入建议界面
            
            PollingIdeaViewController *ideaVC=[PollingIdeaViewController new];
            NSMutableDictionary *dic=[self.allAr objectAtIndex:0];
            NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
            [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
            [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
            [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
            
            NSString * isEdit=[NSString stringWithFormat:@"%d",[[dic objectForKey:@"is_completed"] integerValue]];
            [dataDic setObject:isEdit forKey:@"isEdit"];
            ideaVC.passDic=dataDic;
            ideaVC.allAr=self.allAr;
            ideaVC.Uuid=self.Uuid;

            
            if([self.stateIdea  isEqualToString:@"0"])
            {
                [ideaVC setType:IdeaEdit];
                
            }else
            {
                [ideaVC setType:IdeaDetail];
                
            }
            [self pushController:ideaVC];
            
            
            
        } onError:^(BaseModel *model) {
            
        }];

        
        
        
    }else
    {
        
        NSMutableArray *parameterAr=[[NSMutableArray alloc]init];
        for (int i=0; i<self.dataTableAr.count; i++) {
            
            NSMutableDictionary *upDic=[[NSMutableDictionary alloc]init];//上传的参数
            NSMutableDictionary *askDic=[self.dataTableAr objectAtIndex:i];//请求数据
            [upDic setObject:[askDic objectForKey:@"item_uuid"] forKey:@"item_uuid"];//巡检项uuid
            //找到正常异常
            NSString* num=[self.selectAr objectAtIndex:i];
            //上传到 选择项
            if ([num isEqualToString:@"1"]) {//正常
                [upDic setObject:@"1" forKey:@"result"];
                
            }else if([num isEqualToString:@"2"])//异常
            {
                [upDic setObject:@"2" forKey:@"result"];
                
            }else if ([num isEqualToString:@"0"])//没有填的
            {
                //没有选择 选择一个框
                [upDic setObject:@"" forKey:@"result"];
              
                
            }
            
            NSString* textFiled=[self.textFieldAr objectAtIndex:i];
            [upDic setObject:textFiled  forKey:@"content"];
            [parameterAr addObject:upDic];
            
            
        }
        
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:parameterAr forKey:@"list"];
        [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
        NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            //先保存进入下个界面

            for (int i=0; i<self.allAr.count; i++) {
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                dic=[self.allAr objectAtIndex:i];
                if (_index==[[dic objectForKey:@"order_no"] integerValue]) {
                    
                    //isEdit 0 为没有填完 1为填完
                    PollingTableViewController *networkVC=[PollingTableViewController new];
                    NSMutableDictionary *dic=[self.allAr objectAtIndex:_index-1];
                    NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                    [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                    [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
                    [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
                    NSString * isEdit=[NSString stringWithFormat:@"%d",[[dic objectForKey:@"is_completed"] integerValue]];
                    [dataDic setObject:isEdit forKey:@"isEdit"];
                    networkVC.passDic=dataDic;
                    networkVC.allAr=self.allAr;
                    networkVC.Uuid=self.Uuid;
                    networkVC.stateIdea=self.stateIdea;
                    [self pushController:networkVC];
                    return ;
                    
                }
            }

          
            
        } onError:^(BaseModel *model) {
            
        }];
        
    }

    
    
    
    
    

}
#pragma mark  上一页
- (void)gotoUp
{
    _index--;
    if (_index==0) {
        //返回到表格列表
        [self backController];
    }else
    {
        
        NSMutableArray *parameterAr=[[NSMutableArray alloc]init];
        for (int i=0; i<self.dataTableAr.count; i++) {
            
            NSMutableDictionary *upDic=[[NSMutableDictionary alloc]init];//上传的参数
            NSMutableDictionary *askDic=[self.dataTableAr objectAtIndex:i];//请求数据
            [upDic setObject:[askDic objectForKey:@"item_uuid"] forKey:@"item_uuid"];//巡检项uuid
            //找到正常异常
            NSString* num=[self.selectAr objectAtIndex:i];
            //上传到 选择项
            if ([num isEqualToString:@"1"]) {//正常
                [upDic setObject:@"1" forKey:@"result"];
                
            }else if([num isEqualToString:@"2"])//异常
            {
                [upDic setObject:@"2" forKey:@"result"];
                
            }else if ([num isEqualToString:@"0"])//没有填的
            {
                //没有选择 选择一个框
                [upDic setObject:@"" forKey:@"result"];
                
                
            }
            
            NSString* textFiled=[self.textFieldAr objectAtIndex:i];
            [upDic setObject:textFiled  forKey:@"content"];
            [parameterAr addObject:upDic];
            
            
        }
        
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:parameterAr forKey:@"list"];
        [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
        NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            
            
            //先保存进入上一页界面
            for (int i=0; i<self.allAr.count; i++) {
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                dic=[self.allAr objectAtIndex:i];
                if (_index==[[dic objectForKey:@"order_no"] integerValue]) {
                    //isEdit 0 为没有填完 1为填完
                    PollingTableViewController *networkVC=[PollingTableViewController new];
                    NSMutableDictionary *dic=[self.allAr objectAtIndex:_index-1];
                    NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                    [dataDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                    [dataDic setObject:[dic objectForKey:@"uuid"] forKey:@"uuid"];
                    [dataDic setObject:[NSString stringWithFormat:@"%d",[[dic objectForKey:@"order_no"] integerValue]] forKey:@"order_no"];
                    
                    NSString * isEdit=[NSString stringWithFormat:@"%d",[[dic objectForKey:@"is_completed"] integerValue]];
                    [dataDic setObject:isEdit forKey:@"isEdit"];
                    networkVC.passDic=dataDic;
                    networkVC.allAr=self.allAr;
                    networkVC.Uuid=self.Uuid;
                    [self pushController:networkVC Lever:0 Animated:NO];
                    
        
                    return ;
                    
                }
                
            }
           
            
            
        } onError:^(BaseModel *model) {
            
        }];
        
        
    }

}

@end
